package com.kingsoft.dc.khaos.dsl.spark.udf.model

import java.util.{Date, Locale}

import com.alibaba.fastjson.JSONObject
import org.apache.commons.lang3.time.FastDateFormat

import scala.collection.mutable

/**
  * create by yansu on 2020/04/22 14:28
  */
object Ks3Udf {
  //输入文件日期时间格式
  private val ddMMMyyyyHHmmss: FastDateFormat = FastDateFormat.getInstance("dd/MMM/yyyy:HH:mm:ss Z", Locale.ENGLISH)

  //目标日期格式
  private val yyMMddHHmm: FastDateFormat = FastDateFormat.getInstance("yyMMddHHmm")

  //日期格式
  private val yyMMddHHmmss: FastDateFormat = FastDateFormat.getInstance("yyMMddHHmmss")

  val YYYYMMDDHHMM_TIME_FORMAT = FastDateFormat.getInstance("yyyy/MM/dd HH:mm:ss")
  val TARGET_FORMAT = FastDateFormat.getInstance("yyyyMMddHHmmss")

  /**
    * 获取分钟
    */
  def getMin(minute: String) = {
    minute.substring(10, 12)
  }

  /**
    * 获取秒：00-29属于00   30-59属于30
    */
  def getSecond(minute: String) = {
    val second = minute.substring(12, 14).toInt
    if (second < 30) {
      "00"
    } else {
      "30"
    }
  }


  def get5Min(minute: String) = {
    val m = minute.substring(10, 12).toInt
    if (m < 10)
      "0" + m / 5 * 5
    else
      "" + m / 5 * 5
  }

  def getDefaultValue(value: String, originDefaultValue: String, targetDefaultValue: String) = {
    if (value.trim.equals(originDefaultValue.trim)) targetDefaultValue else value.trim
  }

  def parseToMinute(time: String): String = {
    try{
      TARGET_FORMAT.format(time.toLong * 1000)
    }catch {
      case e:Exception=>
        "197001010000"
    }
  }

  def getLogLength(log: String): Int = {
    Math.max(log.split(" ").length, log.split("\t").length)
  }

  // 时间格式转换 将固定格式的时间转换成Long类型的字符串
  def coverTimeFormatLong(timeStr: String, timeformat: String): Long = {
    try {
      var time = ""
      timeformat match {
        case "dd/MMM/yyyy:HH:mm:ss Z" => {
          if (timeStr.contains("[") && timeStr.contains("]")) {
            time = ddMMMyyyyHHmmss.parse(timeStr.substring(timeStr.indexOf('[') + 1, timeStr.lastIndexOf(']'))).getTime.toString
          } else {
            time = ddMMMyyyyHHmmss.parse(timeStr).getTime.toString
          }
        }
        case "yyMMddHHmm" => {
          if (timeStr.contains("[") && timeStr.contains("]")) {
            time = yyMMddHHmm.parse(timeStr.substring(timeformat.indexOf('[') + 1, timeStr.lastIndexOf(']'))).getTime.toString
          } else {
            time = yyMMddHHmm.parse(timeStr).getTime.toString
          }
        }
        case "yyMMddHHmmss" => {
          if (timeStr.contains("[") && timeStr.contains("]")) {
            time = yyMMddHHmmss.parse(timeStr.substring(timeformat.indexOf('[') + 1, timeStr.lastIndexOf(']'))).getTime.toString
          } else {
            time = yyMMddHHmmss.parse(timeStr).getTime.toString
          }
        }
        case _ =>
      }
      time.toLong
    } catch {
      case e: Exception => {
        0l
      }
    }
  }

  /*时间格式转换
  将固定格式的时间转换成指定格式的字符串类型时间
  例如:13/Apr/2020:23:59:59 +0800 ==> 200413235959
   */
  def coverTimeFormat(timeStr: String, timeformat: String, target: String): String = {
    try {
      var time = 0l
      timeformat match {
        case "dd/MMM/yyyy:HH:mm:ss Z" => {
          if (timeStr.contains("[") && timeStr.contains("]")) {
            time = ddMMMyyyyHHmmss.parse(timeStr.substring(timeStr.indexOf('[') + 1, timeStr.lastIndexOf(']'))).getTime
          } else {
            time = ddMMMyyyyHHmmss.parse(timeStr).getTime
          }
        }
        case "yyMMddHHmm" => {
          if (timeStr.contains("[") && timeStr.contains("]")) {
            time = yyMMddHHmm.parse(timeStr.substring(timeformat.indexOf('[') + 1, timeStr.lastIndexOf(']'))).getTime
          } else {
            time = yyMMddHHmm.parse(timeStr).getTime
          }
        }
        case "yyMMddHHmmss" => {

          if (timeStr.contains("[") && timeStr.contains("]")) {
            time = yyMMddHHmmss.parse(timeStr.substring(timeformat.indexOf('[') + 1, timeStr.lastIndexOf(']'))).getTime
          } else {
            time = yyMMddHHmmss.parse(timeStr).getTime
          }
        }
        case "long" => time = timeStr.toLong
      }
      var time_res_format = ""
      target match {
        case "dd/MMM/yyyy:HH:mm:ss Z" => {
          time_res_format = ddMMMyyyyHHmmss.format(new Date(time))
        }
        case "yyMMddHHmm" => {
          time_res_format = yyMMddHHmm.format(new Date(time))
        }
        case "yyMMddHHmmss" => {
          time_res_format = yyMMddHHmmss.format(new Date(time))
        }
      }
      time_res_format
    } catch {
      case e: Exception => {
        timeStr
      }
    }
  }

  // 获取切割后集合的长度                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               小
  def get_split_size(value: String, separator: String): Int = {
    try {
      var arrg = 0
      if (separator.equalsIgnoreCase("")) {
        arrg = value.split(s"$separator", -1).size
      } else {
        arrg = value.split(s"\\$separator", -1).size
      }
      arrg
    } catch {
      case e: Exception => {
        0
      }
    }
  }

  // 获取切割后某个元素的位置
  def get_split_value(value: String, separator: String, index: String): String = {
    try {
      if (separator.equalsIgnoreCase("")) {
        value.split(s"$separator", -1)(index.toInt - 1)
      } else {
        value.split(s"\\$separator", -1)(index.toInt - 1)
      }
    } catch {
      case e: Exception =>
        value
    }
  }

  // 输入yyMMddHHmmss类型字符串
  def get_second(value: String): String = {
    try {
      val second = value.substring(10, 12).toInt
      if (second < 30) {
        "00"
      } else {
        "30"
      }
    } catch {
      case e: Exception =>
        value
    }
  }

  //yyMMddHHmmss
  def get_minute(value: String, arg: Int): String = {
    try {
      val m = value.substring(8, 10).toInt
      if (m < 10)
        "0" + m / arg * arg
      else
        "" + m / arg * arg
    } catch {
      case e: Exception =>
        value
    }
  }

  def get_code(source: String): String = {
    try {
      val map = new mutable.HashMap[String, String]()
      map += ("LG" -> "RegionOne")
      map += ("YZ" -> "BJYZRegionOne")
      map += ("ZJM" -> "BJZJMRegionOne")
      map += ("SH" -> "SHRegionOne")
      map += ("ZJMXM" -> "BJZJMXMRegion")
      map += ("HK" -> "HKSTRegionOne")
      map += ("USA" -> "USCARegionOne")
      map += ("YZXM" -> "BJYZXMRegion")
      map += ("SHPBS" -> "SHPBSRegionOne")
      map += ("HKVPC" -> "HKVPCRegion")
      map += ("ZJMVPC" -> "BJZJMVPCRegion")
      map += ("WQ" -> "TJWQRegion")
      map.getOrElse(source, source)
    } catch {
      case e: Exception =>
        source
    }
  }

  // 约定key的格式为 get_json('a1|a2',a1,a2)
  def get_json(args: String*): String = {
    try {
      val jsonObj: JSONObject = new JSONObject()
      val keysArr = args.head.split("\\|", -1)
      for (i <- keysArr.indices) {
        jsonObj.put(keysArr(i), args(i + 1))
      }
      jsonObj.toString()
    } catch {
      case e: Exception =>
        null
    }
  }

  def custom(value: String): String = {
    try {
      value
    } catch {
      case e: Exception =>
        value
    }
  }


  /*
  多列拼接
   */
  def concat_plus(args: String*) = {
    val stb = new mutable.StringBuilder()
    for (arg <- args) {
      stb.append(arg)
    }
    stb.toString()
  }

  /*
  多列自定义拼接 'a1|a2|a3',a1,a2,a3,'=',','
   */
  def concat_custom(arg1: String, arg: String*): String = {
    val fieldNames: Array[String] = arg1.split("\\|", -1)
    val md5Hex = new mutable.StringBuilder()
    val fieldSize = fieldNames.length
    for (i <- 0 to fieldSize - 1) {
      if (i != fieldSize - 1) {
        md5Hex.append(fieldNames(i)).append(arg(arg.size - 2)).append(arg(i)).append(arg(arg.size - 1))
      } else {
        md5Hex.append(fieldNames(i)).append(arg(arg.size - 2)).append(arg(i))
      }
    }
    md5Hex.toString()
  }

}
